package com.example.administrator.k3xiangmu;

import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.example.administrator.k3xiangmu.adapter.ShengchanAdapter;
import com.example.administrator.k3xiangmu.entity.ShengchangrenwudanEntity;
import com.example.administrator.k3xiangmu.fragment.DaikaigongFragment;
import com.example.administrator.k3xiangmu.fragment.ShengchanzhongFragment;
import com.example.administrator.k3xiangmu.fragment.YiwangongFragment;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class MainActivity extends AppCompatActivity {
    public  static  String ip = "205.168.1.123:1433";
    public  static String userName = "k3";

    public  static String userPwd = "123456";
    public  static String userDB = "erp";

    private Button bt1, bt2, bt3;
    private List<ShengchangrenwudanEntity>list=new ArrayList<>();

    private DaikaigongFragment fragment1;
    private ShengchanzhongFragment fragment2;
    private YiwangongFragment fragment3;
    private int a=0,b=0,c=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_main);
        setView();
        setListen();
        //getData();
    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void setListen() {
     final    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        fragment1 = new DaikaigongFragment();
        fragment2=new ShengchanzhongFragment();
        fragment3=new YiwangongFragment();
        FragmentTransaction ft = fm.beginTransaction();

        ft.add(R.id.fragment, fragment1).commit();

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!fragment1.isVisible()){
                    FragmentTransaction ft= fm.beginTransaction();
                    ft.replace(R.id.fragment,fragment1);
                    ft.commit();
                }

            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!fragment2.isVisible()){
                    FragmentTransaction ft= fm.beginTransaction();
                    ft.replace(R.id.fragment,fragment2);
                    ft.commit();
                }
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!fragment3.isVisible()){
                    FragmentTransaction ft= fm.beginTransaction();
                    ft.replace(R.id.fragment,fragment3);
                    ft.commit();
                }
            }
        });
    }

    private void setView() {
       // lv=(ListView)findViewById(R.id.listview);

        bt1 = (Button) findViewById(R.id.bt_1);
        bt2 = (Button) findViewById(R.id.bt_2);
        bt3 = (Button) findViewById(R.id.bt_3);

    }




    public void onEventMainThread(Event event)
    {
        if(event.getFlag()==1){
            a--;
            b++;
            bt1.setText("待开工（"+a+"）");
            bt2.setText("生产中（"+b+"）");
        }else if(event.getFlag()==2){
            b--;
            c++;
            bt2.setText("生产中（"+b+"）");
            bt3.setText("已完工（"+c+"）");

        }else  {
        a=event.getA();
        b=event.getB();
        c=event.getC();
        bt1.setText("待开工（"+event.getA()+"）");
        bt2.setText("生产中（"+event.getB()+"）");
        bt3.setText("已完工（"+event.getC()+"）");}
    }
}
